
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.4Method-Swizzling · zhoufei-iosknowledges</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="zhoufei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../neicun/4.html" />
    
    
    <link rel="prev" href="3.3.html" />
    

    
        <link rel="shortcut icon" href='../../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../../source/images/apple-touch-icon.jpg'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../../source/images/apple-touch-icon.jpg">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../../source/images/apple-touch-icon.jpg">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"zhou415770319","repo":"iosKnowledges","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.feiyum.cn/index/" target="_blank" class="custom-link">我的博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    iOS面试题总结
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../ui/ui.html">
            
                <a href="../ui/ui.html">
            
                    
                    1、UI视图相关面试题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../ui/UITableview.html">
            
                <a href="../ui/UITableview.html">
            
                    
                    1.1UITableview相关面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../ui/shujutongbu.html">
            
                <a href="../ui/shujutongbu.html">
            
                    
                    1.2数据源同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../ui/shijianchuandi.html">
            
                <a href="../ui/shijianchuandi.html">
            
                    
                    1.3UI事件传递与响应
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../ui/tuxiangxianshi.html">
            
                <a href="../ui/tuxiangxianshi.html">
            
                    
                    1.4UI图像显示原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../ui/kadundiaozheng.html">
            
                <a href="../ui/kadundiaozheng.html">
            
                    
                    1.5UI卡顿掉帧原因
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../ui/yibuhuizhi.html">
            
                <a href="../ui/yibuhuizhi.html">
            
                    
                    1.6异步绘制原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../ui/lipingxuanran.html">
            
                <a href="../ui/lipingxuanran.html">
            
                    
                    1.7离屏渲染
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../oc/2.html">
            
                <a href="../oc/2.html">
            
                    
                    2、OC语言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../oc/2.1.html">
            
                <a href="../oc/2.1.html">
            
                    
                    2.1OC相关面试题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../oc/2.2.html">
            
                <a href="../oc/2.2.html">
            
                    
                    2.2分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../oc/2_4.html">
            
                <a href="../oc/2_4.html">
            
                    
                    2.4扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../oc/2.5.html">
            
                <a href="../oc/2.5.html">
            
                    
                    2.5代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../oc/2.6.html">
            
                <a href="../oc/2.6.html">
            
                    
                    2.6通知
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../oc/2.7.html">
            
                <a href="../oc/2.7.html">
            
                    
                    2.7KVO
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../oc/2.8.html">
            
                <a href="../oc/2.8.html">
            
                    
                    2.8KVC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../oc/2.9.html">
            
                <a href="../oc/2.9.html">
            
                    
                    2.9属性关键字
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="3.html">
            
                <a href="3.html">
            
                    
                    3、Runtime
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.1.html">
            
                <a href="3.1.html">
            
                    
                    3.1对象、类对象、元类对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="3.2.html">
            
                <a href="3.2.html">
            
                    
                    3.2消息传递机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="3.3.html">
            
                <a href="3.3.html">
            
                    
                    3.3消息转发流程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.4" data-path="3.4.html">
            
                <a href="3.4.html">
            
                    
                    3.4Method-Swizzling
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../neicun/4.html">
            
                <a href="../neicun/4.html">
            
                    
                    4、内存
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../neicun/4.1.html">
            
                <a href="../neicun/4.1.html">
            
                    
                    4.1引用计数表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../neicun/4.2.html">
            
                <a href="../neicun/4.2.html">
            
                    
                    4.2弱引用表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../neicun/4.3.html">
            
                <a href="../neicun/4.3.html">
            
                    
                    4.3ARC、MRC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../neicun/4.4.html">
            
                <a href="../neicun/4.4.html">
            
                    
                    4.4循环引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../neicun/4.5.html">
            
                <a href="../neicun/4.5.html">
            
                    
                    4.5内存管理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../block/5.html">
            
                <a href="../block/5.html">
            
                    
                    5、Block
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../block/5.1.html">
            
                <a href="../block/5.1.html">
            
                    
                    5.1Block本质
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../block/5.2.html">
            
                <a href="../block/5.2.html">
            
                    
                    5.2截获变量特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../block/5.3.html">
            
                <a href="../block/5.3.html">
            
                    
                    5.3内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../block/5.4.html">
            
                <a href="../block/5.4.html">
            
                    
                    5.4循环引用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../duoxiancheng/6.html">
            
                <a href="../duoxiancheng/6.html">
            
                    
                    6、多线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../duoxiancheng/6.1.html">
            
                <a href="../duoxiancheng/6.1.html">
            
                    
                    6.1GCD、nsoperation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../duoxiancheng/6.2.html">
            
                <a href="../duoxiancheng/6.2.html">
            
                    
                    6.2资源共享
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../duoxiancheng/6.3.html">
            
                <a href="../duoxiancheng/6.3.html">
            
                    
                    6.3线程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../duoxiancheng/6.4.html">
            
                <a href="../duoxiancheng/6.4.html">
            
                    
                    6.4SpinLock等
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../runloop/7.md">
            
                <span>
            
                    
                    7、RunLoop
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../runloop/7.1.md">
            
                <span>
            
                    
                    7.1事件循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../runloop/7.2.md">
            
                <span>
            
                    
                    7.2用户态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../runloop/7.3.md">
            
                <span>
            
                    
                    7.3核心态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../runloop/7.4.md">
            
                <span>
            
                    
                    7.4常驻线程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../network/8.md">
            
                <span>
            
                    
                    8、网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../network/8.1.md">
            
                <span>
            
                    
                    8.1HTTPS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../network/8.2.md">
            
                <span>
            
                    
                    8.2对称加密算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../network/8.3.md">
            
                <span>
            
                    
                    8.3非对称加密
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../network/8.4.md">
            
                <span>
            
                    
                    8.4DNS解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../network/8.5.md">
            
                <span>
            
                    
                    8.5HTTP、TCP
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >3.4Method-Swizzling</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#method-swizzling"><b></b>Method-Swizzling</a></li><li><span class="title-icon "></span><a href="#&#x524D;&#x8A00;"><b></b>&#x524D;&#x8A00;</a></li><li><span class="title-icon "></span><a href="#method-swizzling-&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;"><b></b>Method Swizzling &#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;</a></li><ul><li><span class="title-icon "></span><a href="#1&#x3001;&#x4F7F;&#x7528;-classaddmethod&#x3001;classreplacemethod&#x3001;methodexchangeimplementations"><b></b>1&#x3001;&#x4F7F;&#x7528; class_addMethod&#x3001;class_replaceMethod&#x3001;method_exchangeImplementations</a></li><li><span class="title-icon "></span><a href="#2&#x3001;&#x4F7F;&#x7528;-rsswizzle-&#x5E93;"><b></b>2&#x3001;&#x4F7F;&#x7528; RSSwizzle &#x5E93;</a></li><li><span class="title-icon "></span><a href="#3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;"><b></b>3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;</a></li></ul><li><span class="title-icon "></span><a href="#method-swizzling-&#x6CE8;&#x610F;&#x4E8B;&#x9879;"><b></b>Method Swizzling &#x6CE8;&#x610F;&#x4E8B;&#x9879;</a></li><li><span class="title-icon "></span><a href="#&#x603B;&#x7ED3;"><b></b>&#x603B;&#x7ED3;</a></li></ul></ul></div><a href="#" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h2 id="method-swizzling"><a name="method-swizzling" class="anchor-navigation-ex-anchor" href="#method-swizzling"><i class="fa fa-link" aria-hidden="true"></i></a><a name="method-swizzling" class="plugin-anchor" href="#method-swizzling"><i class="fa fa-link" aria-hidden="true"></i></a>Method-Swizzling</h2>
<h2 id="&#x524D;&#x8A00;"><a name="&#x524D;&#x8A00;" class="anchor-navigation-ex-anchor" href="#&#x524D;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x524D;&#x8A00;" class="plugin-anchor" href="#&#x524D;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x524D;&#x8A00;</h2>
<p>Objective-C &#x662F;&#x4E00;&#x95E8;&#x52A8;&#x6001;&#x8BED;&#x8A00;&#xFF0C;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x671F;&#x624D;&#x80FD;&#x771F;&#x6B63;&#x5177;&#x4F53;&#x786E;&#x5B9A;&#x8C03;&#x7528;&#x54EA;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x65B9;&#x6CD5;&#x7684;&#x5927;&#x6982;&#x8C03;&#x7528;&#x903B;&#x8F91;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>&#x5728;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x7C7B;&#xFF08;Class&#xFF09;&#x7EF4;&#x62A4;&#x4E86;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x5206;&#x53D1;&#x5217;&#x8868;&#x6765;&#x89E3;&#x51B3;&#x6D88;&#x606F;&#x7684;&#x6B63;&#x786E;&#x53D1;&#x9001;&#x3002;&#x6BCF;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x5217;&#x8868;&#x7684;&#x5165;&#x53E3;&#x662F;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF08;Method&#xFF09;&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x6620;&#x5C04;&#x4E86;&#x4E00;&#x5BF9;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x5176;&#x4E2D;&#x952E;&#x503C;&#x662F;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x540D;&#x5B57; selector&#xFF08;SEL&#xFF09;&#xFF0C;&#x503C;&#x662F;&#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5B9E;&#x73B0;&#x7684;&#x51FD;&#x6570;&#x6307;&#x9488; implementation&#xFF08;IMP&#xFF09;&#x3002;</p>
<p>Method swizzling &#x4FEE;&#x6539;&#x4E86;&#x7C7B;&#x7684;&#x6D88;&#x606F;&#x5206;&#x53D1;&#x5217;&#x8868;&#x4F7F;&#x5F97;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684; selector &#x6620;&#x5C04;&#x4E86;&#x53E6;&#x4E00;&#x4E2A;&#x5B9E;&#x73B0; implementation&#xFF0C;&#x540C;&#x65F6;&#x91CD;&#x547D;&#x540D;&#x4E86;&#x539F;&#x751F;&#x65B9;&#x6CD5;&#x7684;&#x5B9E;&#x73B0;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x7684; selector&#x3002;</p>
<p>&#x6458;&#x81EA;&#xFF1A;<a href="https://nshipster.cn/method-swizzling/" target="_blank">Method Swizzling</a></p>
</blockquote>
<p>&#x7B80;&#x5355;&#x6765;&#x8BF4; Method Swizzling &#x5C31;&#x662F;&#x66FF;&#x6362;&#x65B9;&#x6CD5;&#x5217;&#x8868;&#x4E2D;&#x7684; selector &#x5BF9;&#x5E94;&#x7684; IMP&#xFF0C;&#x8FBE;&#x5230;&#x65B9;&#x6CD5;&#x4EA4;&#x4E92;&#x7684;&#x76EE;&#x7684;&#x3002;</p>
<h2 id="method-swizzling-&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;"><a name="method-swizzling-&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;" class="anchor-navigation-ex-anchor" href="#method-swizzling-&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="method-swizzling-&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;" class="plugin-anchor" href="#method-swizzling-&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;"><i class="fa fa-link" aria-hidden="true"></i></a>Method Swizzling &#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x6848;</h2>
<h3 id="1&#x3001;&#x4F7F;&#x7528;-classaddmethod&#x3001;classreplacemethod&#x3001;methodexchangeimplementations"><a name="1&#x3001;&#x4F7F;&#x7528;-classaddmethod&#x3001;classreplacemethod&#x3001;methodexchangeimplementations" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x4F7F;&#x7528;-classaddmethod&#x3001;classreplacemethod&#x3001;methodexchangeimplementations"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x4F7F;&#x7528;-classaddmethod&#x3001;classreplacemethod&#x3001;methodexchangeimplementations" class="plugin-anchor" href="#1&#x3001;&#x4F7F;&#x7528;-classaddmethod&#x3001;classreplacemethod&#x3001;methodexchangeimplementations"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x4F7F;&#x7528; class_addMethod&#x3001;class_replaceMethod&#x3001;method_exchangeImplementations</h3>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7CFB;&#x7EDF;&#x63D0;&#x4F9B;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x64CD;&#x4F5C;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;&#x5217;&#x8868;&#xFF0C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>    SEL originalSeletor = originalSelector;
    SEL swizzledSeletor = swizzledSelector;

    Method originalMethod = class_getInstanceMethod(originalClass, originalSeletor);
    Method swizzledMethod = class_getInstanceMethod(swizzledClass, swizzledSeletor);
    if (!isInstanceMethod) {
        originalMethod = class_getClassMethod(originalClass, originalSeletor);
        swizzledMethod = class_getClassMethod(swizzledClass, swizzledSeletor);
    }

        //&#x5148;&#x5C1D;&#x8BD5;&#x7D66;&#x6E90;SEL&#x6DFB;&#x52A0;IMP&#xFF0C;&#x8FD9;&#x91CC;&#x662F;&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x6E90;SEL&#x6CA1;&#x6709;&#x5B9E;&#x73B0;IMP&#x7684;&#x60C5;&#x51B5;
    BOOL didAddMethod = class_addMethod(originalClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        // class_addMethod &#x6DFB;&#x52A0;&#x6210;&#x529F;&#x5219;&#x8BF4;&#x660E;&#xFF1A;originalClass &#x6CA1;&#x6709;&#x5B9E;&#x73B0; originalSelector &#x5BF9;&#x5E94;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x6B64;&#x65F6;&#x5DF2;&#x7ECF;&#x5C06; originalSelector &#x5BF9;&#x5E94;&#x5B9E;&#x73B0;&#x6539;&#x6210; swizzledMethod &#x7684;&#x5B9E;&#x73B0;&#x3002;
        //&#x6DFB;&#x52A0;&#x6210;&#x529F;&#xFF1A;&#x8BF4;&#x660E;&#x6E90;SEL&#x6CA1;&#x6709;&#x5B9E;&#x73B0;IMP&#xFF0C;&#x5C06;&#x6E90;SEL&#x7684;IMP&#x66FF;&#x6362;&#x5230;&#x4EA4;&#x6362;SEL&#x7684;IMP
        class_replaceMethod(swizzledClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        // class_addMethod &#x6DFB;&#x52A0;&#x5931;&#x8D25;&#x5219;&#x8BF4;&#x660E;&#xFF1A;originalClass &#x5DF2;&#x7ECF;&#x5B9E;&#x73B0; originalSelector &#x5BF9;&#x5E94;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x80FD;&#x518D;&#x52A0;&#x4E86;&#xFF0C;&#x8FD9;&#x91CC;&#x76F4;&#x63A5;&#x4EA4;&#x6362;&#x5B9E;&#x73B0;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;
        //&#x6DFB;&#x52A0;&#x5931;&#x8D25;&#xFF1A;&#x8BF4;&#x660E;&#x6E90;SEL&#x5DF2;&#x7ECF;&#x6709;IMP&#xFF0C;&#x76F4;&#x63A5;&#x5C06;&#x4E24;&#x4E2A;SEL&#x7684;IMP&#x4EA4;&#x6362;&#x5373;&#x53EF;
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
</code></pre><h3 id="2&#x3001;&#x4F7F;&#x7528;-rsswizzle-&#x5E93;"><a name="2&#x3001;&#x4F7F;&#x7528;-rsswizzle-&#x5E93;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x4F7F;&#x7528;-rsswizzle-&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x4F7F;&#x7528;-rsswizzle-&#x5E93;" class="plugin-anchor" href="#2&#x3001;&#x4F7F;&#x7528;-rsswizzle-&#x5E93;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x4F7F;&#x7528; RSSwizzle &#x5E93;</h3>
<p>&#x5BF9;&#x4E8E;&#x4E0A;&#x9762;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#xFF0C;&#x5728;&#x4E00;&#x822C;&#x60C5;&#x51B5;&#x4E0B;&#x662F;OK&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x573A;&#x666F;&#x7279;&#x522B;&#x590D;&#x6742;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#xFF0C;&#x4F8B;&#x5982;&#x5B50;&#x7C7B;&#x3001;&#x7236;&#x7C7B;&#x3001;&#x5206;&#x7C7B;&#x591A;&#x4E2A;&#x7C7B;&#x540C;&#x65F6;&#x5BF9;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x4EA4;&#x6362;&#xFF0C;&#x8FD9;&#x91CC;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#x95EE;&#x9898;&#xFF0C;&#x5177;&#x4F53;&#x6BD4;&#x8F83;&#x53EF;&#x4EE5;&#x53C2;&#x9605;&#xFF1A;<a href="http://yulingtianxia.com/blog/2017/04/17/Objective-C-Method-Swizzling/" target="_blank">Objective-C Method Swizzling</a>&#x3001;<a href="https://juejin.im/entry/5a1fceddf265da43310d9985" target="_blank">iOS &#x754C;&#x7684;&#x6BD2;&#x7624;&#xFF1A;Method Swizzle</a> &#x4E2D;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<p>RSSwizzle&#x7684;&#x4E3B;&#x8981;&#x601D;&#x8DEF;&#x662F; hook &#x672C;&#x7C7B;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x7684;&#x65B9;&#x6CD5;(&#x7EE7;&#x627F;&#x4E86;&#x7236;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;),&#x4E0D;&#x4F1A;&#x53BB;copy&#x7236;&#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x5E76;&#x6DFB;&#x52A0;&#x65B9;&#x6CD5;&#x5230;&#x672C;&#x7C7B;&#x4E2D;&#xFF0C;&#x800C;&#x662F;&#x5728;&#x6D88;&#x606F;&#x89E6;&#x53D1;&#x65F6;&#x52A8;&#x6001;&#x53BB;&#x7236;&#x7C7B;&#x4E2D;&#x67E5;&#x627E;&#x4EE5;&#x8C03;&#x7528;&#x539F;&#x6765;&#x7684;&#x5B9E;&#x73B0;&#x3002;</p>
<h3 id="3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;"><a name="3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;" class="anchor-navigation-ex-anchor" href="#3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;" class="plugin-anchor" href="#3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;"><i class="fa fa-link" aria-hidden="true"></i></a>3&#x3001;&#x5982;&#x4F55;&#x9009;&#x62E9;</h3>
<p>&#x4E2A;&#x4EBA;&#x89C9;&#x5F97;&#x5BF9;&#x4E8E;&#x5C0F;&#x8303;&#x56F4;&#x529F;&#x80FD;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x4E0D;&#x4F1A;&#x591A;&#x6B21;&#x66FF;&#x6362;&#x65B9;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x65B9;&#x6848;&#x4E00;&#x76F4;&#x63A5;&#x8FDB;&#x884C;&#x65B9;&#x6CD5;&#x66FF;&#x6362;&#xFF1B;&#x4F46;&#x662F;&#x5BF9;&#x4E8E;&#x590D;&#x6742;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528; RSSwizzle &#x5E93;&#x6765;&#x8FDB;&#x884C;&#x65B9;&#x6CD5;&#x66FF;&#x6362;&#x3002;</p>
<h2 id="method-swizzling-&#x6CE8;&#x610F;&#x4E8B;&#x9879;"><a name="method-swizzling-&#x6CE8;&#x610F;&#x4E8B;&#x9879;" class="anchor-navigation-ex-anchor" href="#method-swizzling-&#x6CE8;&#x610F;&#x4E8B;&#x9879;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="method-swizzling-&#x6CE8;&#x610F;&#x4E8B;&#x9879;" class="plugin-anchor" href="#method-swizzling-&#x6CE8;&#x610F;&#x4E8B;&#x9879;"><i class="fa fa-link" aria-hidden="true"></i></a>Method Swizzling &#x6CE8;&#x610F;&#x4E8B;&#x9879;</h2>
<p>Method Swizzling &#x662F;&#x4E00;&#x628A;&#x53CC;&#x5203;&#x5251;&#xFF0C;&#x4F7F;&#x7528;&#x4E0D;&#x5F53;&#x4E5F;&#x4F1A;&#x7ED9;&#x9879;&#x76EE;&#x5E26;&#x6765;&#x9690;&#x60A3;&#xFF0C;&#x8FD9;&#x91CC;&#x63D0;&#x4E00;&#x4E9B;&#x6CE8;&#x610F;&#x4E8B;&#x9879;&#xFF1A;</p>
<p>1&#x3001;&#x5C3D;&#x91CF;&#x5728; +load &#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x5E76;&#x4E14;&#x7528; dispatch_once &#x6765;&#x786E;&#x4FDD;&#x4EA4;&#x6362;&#x987A;&#x5E8F;&#x548C;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;</p>
<p>2&#x3001;&#x5BF9;&#x4E8E; Hook &#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x9700;&#x8981;&#x8C03;&#x7528;&#x539F;&#x6709;&#x5B9E;&#x73B0;&#xFF0C;&#x907F;&#x514D;&#x8986;&#x76D6;&#x539F;&#x6709;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5BFC;&#x81F4;&#x539F;&#x6709;&#x7684;&#x529F;&#x80FD;&#x7F3A;&#x5931;</p>
<p>3&#x3001;&#x65B9;&#x6CD5;&#x547D;&#x540D;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x524D;&#x7F00;&#xFF0C;&#x907F;&#x514D;&#x4E0E;&#x539F;&#x65B9;&#x6CD5;&#x540D;&#x5B57;&#x51B2;&#x7A81;</p>
<h2 id="&#x603B;&#x7ED3;"><a name="&#x603B;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x603B;&#x7ED3;" class="plugin-anchor" href="#&#x603B;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x603B;&#x7ED3;</h2>
<p>Method Swizzling &#x76F8;&#x5BF9;&#x4E8E;&#x662F;&#x6BD4;&#x8F83;&#x5E95;&#x5C42;&#x7684;&#x6280;&#x672F;&#xFF0C;&#x5E73;&#x65F6;&#x5F00;&#x53D1;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x4E0D;&#x591A;&#xFF0C;&#x4F46;&#x662F;&#x9047;&#x5230;&#x7279;&#x5B9A;&#x7684;&#x573A;&#x666F; Method Swizzling &#x5374;&#x53EF;&#x4EE5;&#x5F88;&#x4F18;&#x96C5;&#x7684;&#x89E3;&#x51B3;&#x95EE;&#x9898;&#xFF1B;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x638C;&#x63E1; Method Swizzling &#x7684;&#x5B9E;&#x73B0;&#x539F;&#x7406;&#xFF0C;&#x907F;&#x514D;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x6EE5;&#x7528;&#xFF0C;&#x4E0D;&#x7136;&#x4F1A;&#x5BFC;&#x81F4;&#x5F88;&#x96BE;&#x6392;&#x67E5;&#x7684; Bug&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.3.html" class="navigation navigation-prev " aria-label="Previous page: 3.3消息转发流程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../neicun/4.html" class="navigation navigation-next " aria-label="Next page: 4、内存">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.4Method-Swizzling","level":"1.4.4","depth":2,"next":{"title":"4、内存","level":"1.5","depth":1,"path":"md/neicun/4.md","ref":"md/neicun/4.md","articles":[{"title":"4.1引用计数表","level":"1.5.1","depth":2,"path":"md/neicun/4.1.md","ref":"md/neicun/4.1.md","articles":[]},{"title":"4.2弱引用表","level":"1.5.2","depth":2,"path":"md/neicun/4.2.md","ref":"md/neicun/4.2.md","articles":[]},{"title":"4.3ARC、MRC","level":"1.5.3","depth":2,"path":"md/neicun/4.3.md","ref":"md/neicun/4.3.md","articles":[]},{"title":"4.4循环引用","level":"1.5.4","depth":2,"path":"md/neicun/4.4.md","ref":"md/neicun/4.4.md","articles":[]},{"title":"4.5内存管理","level":"1.5.5","depth":2,"path":"md/neicun/4.5.md","ref":"md/neicun/4.5.md","articles":[]}]},"previous":{"title":"3.3消息转发流程","level":"1.4.3","depth":2,"path":"md/runtime/3.3.md","ref":"md/runtime/3.3.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","github","github-buttons","donate","sharing-plus","anchor-navigation-ex","favicon","livereload"],"styles":{"website":"./styles/website.css","ebook":"styles/ebook.css","pdf":"styles/pdf.css","mobi":"styles/mobi.css","epub":"styles/epub.css"},"pluginsConfig":{"github":{"url":"https://github.com/zhou415770319"},"livereload":{},"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"donate":{"alipay":"../../img/alipay.JPG","alipayText":" ","button":"赞赏","title":"","wechat":"","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{"shortcut":"./source/images/favicon.jpg","bookmark":"./source/images/favicon.jpg","appleTouch":"./source/images/apple-touch-icon.jpg","appleTouchMore":{"120x120":"./source/images/apple-touch-icon.jpg","180x180":"./source/images/apple-touch-icon.jpg"}},"github-buttons":{"buttons":[{"user":"zhou415770319","repo":"iosKnowledges","type":"star","size":"small","count":true}]},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","author":"zhoufei","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"zhoufei-iosknowledges","language":"zh-hans","links":{"sidebar":{"我的博客":"http://www.feiyum.cn/index/"}},"gitbook":"3.2.3","description":"ios面试知识点总结"},"file":{"path":"md/runtime/3.4.md","mtime":"2019-05-03T14:40:51.320Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-03T14:51:39.741Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

